import tkinter as tk
from PIL import Image,ImageTk
from random import randint as r
import sqlite3
#获取图片
def get_image(filename,width,height):
    im = Image.open(filename).resize((width,height))
    return ImageTk.PhotoImage(im)
#冒泡排序
def sort2(a):
    for i in range(len(a)):
        for j in range(i, len(a)):
            if a[i] > a[j]:
                a[i], a[j] = a[j], a[i]
            if len(a)>=2:
                if a[-1] < a[-2]:
                    a[-1],a[-2]=a[-2],a[-1]
    return a
#数据查询
def select1(b): 
    try:                                                       #数据查询(输入参数统一为用户名或ID，空值为查询所有)
        db = sqlite3.connect('data.db')
        cur = db.cursor()
        if b == "":
            sel = "SELECT * FROM task"
            cur.execute(sel)
        else:
            sel = "SELECT * FROM task where id == ? or name == ?"
            cur.execute(sel,(b,b))  
        db.commit()
        a=cur.fetchall()
        print(a)
        if len(a) > 1:
            a=sort2(a)
        cur.close()
        db.close()
        return a
    except:
        print('afmjfdvnf')
        return 0
    

creative= tk.Tk()
creative.title('创建任务')
creative.geometry('512x364')
v = tk.IntVar()
#设置背景
canvas_root = tk.Canvas(creative,width=512,height=364)
im_root = get_image('photo/bg141.png',512,364)
canvas_root.create_image(256,182,image = im_root)
#输入组件
E_select = tk.Entry(bg='white',bd=1,cursor='arrow',font='宋体',fg='black',width=40,exportselection=0,highlightcolor='blue',textvariable='请输入查询用户ID或者用户名(请勿同时输入)',xscrollcommand=1)
image1 = get_image('photo/qd.png',95,35)
select = E_select.get()
button = tk.Button(image=image1,compound=tk.CENTER,command=lambda:select1(select))
#组件布局
canvas_root.pack()
E_select.place(x=20,y=80)
button.place(x=400,y=320)

creative.mainloop()